package com.zr.subclass.service.impl;


import com.zr.meiju.StatusEnum;
import com.zr.subclass.mapper.SubClassMapper;
import com.zr.subclass.pojo.*;
import com.zr.subclass.service.SubClassService;
import com.zr.util.AllRecords;
import com.zr.util.ResultBuildVo;
import com.zr.util.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by 86151 on 2019/6/14.
 */
@Service
@Slf4j
public class SubClassServiceImpl implements SubClassService {
    @Autowired
    private SubClassMapper subClassMapper;


    @Transactional
    @Override
    public ResultVo addSubClass(SubClassAddVo subClassAddVo) {
        //逻辑验证
        ResultVo assigment = assigment(subClassAddVo);
        if (!assigment.isSuccess()){
            return assigment;
//            return ResultBuildVo.error(assigment.getErrorMessage(),assigment.getErrorCode());
        }
        //给数据库中的创建时间和修改时间等信息赋值
        SubClass subClass = new SubClass();
        Date nowDate = new Date();
        subClass.setCreateTime(nowDate);
        subClass.setCreateName("小明");
        subClass.setUpdateTime(nowDate);
        subClass.setUpdateName("小明");
        //把左边对象的数据赋值给右边对象的数据
        //注意必须相同参数名称和相同参数类型才能自动进行赋值
        BeanUtils.copyProperties(subClassAddVo,subClass);
        subClassMapper.addSubClass(subClass);
        return ResultBuildVo.success(subClassAddVo);
    }

    @Override
    public ResultVo<SubClass> querySubClassById(Integer id) {
        if (id==null){
            return ResultBuildVo.error("查看时id不能为空！","500");
        }
        SubClass subClass = subClassMapper.queryById(id);
        if (subClass==null){
            return ResultBuildVo.error("查看对象不存在！","500");
        }
        return ResultBuildVo.success(subClass);
    }



    @Override
    public ResultVo<AllRecords> queryPage(SubClassSelectVo subClassSelectVo) {
        //查询数据
        List<SubClass> planClassList = subClassMapper.queryPage(subClassSelectVo);
        for (SubClass planClass:planClassList){
            Integer status = planClass.getStatus()==true?1:0;
            planClass.setStatusName(StatusEnum.getName(status));

        }
        //查询数量
       int count =  subClassMapper.queryCount(subClassSelectVo);
        //把当前页、每页大小、总页数、总条数、数据统一放入到AllRecords返回
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(subClassSelectVo.getPageIndex());
        allRecords.setPageSize(subClassSelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        allRecords.setDataList(planClassList);
        return ResultBuildVo.success(allRecords);
    }

    @Override
    public ResultVo updateStatus(SubClassStatusVo subClassStatusVo) {
        //逻辑验证
        //验证数据是否存在
        SubClass queryPlantById = subClassMapper.querySubClassById(subClassStatusVo.getId());
        if (queryPlantById==null){
            return ResultBuildVo.error("修改数据不存在！","500");
        }
        //给数据库中的创建时间和修改时间等信息赋值
        SubClass subClass = new SubClass();
        Date nowDate = new Date();
        subClass.setUpdateTime(nowDate);
        subClass.setUpdateName("小明");
        //把左边对象的数据赋值给右边对象的数据
        //注意必须相同参数名称和相同参数类型才能自动进行赋值
        BeanUtils.copyProperties(subClassStatusVo, subClass);
        subClassMapper.updateStatus(subClass);
        return ResultBuildVo.success(subClass);
    }

    @Override
    public ResultVo updatePlant(SubClassUpdateVo subClassUpdateVo) {

        //逻辑验证=禁用状态下才允许修改
        ResultVo assigment = assigmentUpdate(subClassUpdateVo);
        if (!assigment.isSuccess()){
            return assigment;
        }
        //给数据库中的创建时间和修改时间等信息赋值
        SubClass subClass = new SubClass();
        Date nowDate = new Date();
        subClass.setUpdateTime(nowDate);
        subClass.setUpdateName("小明");
        //把左边对象的数据赋值给右边对象的数据
        //注意必须相同参数名称和相同参数类型才能自动进行赋值
        BeanUtils.copyProperties(subClassUpdateVo, subClass);
        subClassMapper.updatePlant(subClass);
        return ResultBuildVo.success(subClassUpdateVo);
    }

    public ResultVo assigment(SubClassAddVo subClassAddVo){
        //        1.验证编码是否已经存在
        List<SubClass> subClassesCode = subClassMapper.queryByCode(subClassAddVo.getCode());
        if (!CollectionUtils.isEmpty(subClassesCode)){
            return ResultBuildVo.error("已经存在编码！","500");
        }
//        1.验证名称是否已经存在
        List<SubClass> subClassesName = subClassMapper.queryByName(subClassAddVo.getName());
        if (!CollectionUtils.isEmpty(subClassesName)){
            return ResultBuildVo.error("已经存在名称！","500");
        }
        return ResultBuildVo.success(subClassAddVo);
    }


    public ResultVo assigmentUpdate(SubClassUpdateVo subClassUpdateVo){
        //验证数据是否存在
        List<SubClass> plantClasses = subClassMapper.queryByCode(subClassUpdateVo.getCode());
        if (CollectionUtils.isEmpty(plantClasses)){
            return ResultBuildVo.error("修改数据不存在！","500");
        }
        //1.验证名称是否已经存在
        List<SubClass> plantClassesList = subClassMapper.queryByAll(subClassUpdateVo);
        if (!CollectionUtils.isEmpty(plantClassesList)){
            return ResultBuildVo.error("已经存在名称！","500");
        }
       /* //禁用状态下才允许修改
        if (plantClass.getStatus()== StatusEnum.QIYONG.getStatusValue()){
            return ResultBuildVo.error("禁用状态才能修改！","500");
        }*/
        return ResultBuildVo.success(subClassUpdateVo);
    }
}
